import 'dart:convert';

import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

//足迹控制器
class FootprintController extends BaseController {
  // RefreshController baseRefreshCtr=RefreshController(initialRefresh: false);

  final ScrollController scrollController = ScrollController();

  String actionTxt = '编辑';

  bool showButton = true;

  int page = 1;

  bool selectedAll = false;

  //选中的商品id数组
  List<String> goodsIdList = [];

  List<Map<String, dynamic>> data = [];

  List keys = [];

  @override
  void onInit() {
    getHistory();
    super.onInit();
  }

  void getHistory({bool one = true}) async {
    Tools.showLoading();
    await Network.getHistory().then((value) {
      logger.i('value === ${value['historyInfo']}');
      data = List<Map<String, dynamic>>.from(value['historyInfo']);
      logger.i('看看data  === $data');
      for (var item in data) {
        List<Map<String, dynamic>> viewGoods = List<Map<String, dynamic>>.from(item['list']);
        for (var detail in viewGoods) {
          detail['isSelect'] = 0;
        }
      }
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  ///全选反选
  void selectedAllItem(bool select) {
    if (data.isNotEmpty) {
      selectedAll = select;

      int isSelect;
      if (select) {
        isSelect = 1;
      } else {
        isSelect = 0;
      }

      data.forEach((item) {
        List<Map<String, dynamic>> viewGoods = List<Map<String, dynamic>>.from(item['list']);
        viewGoods.forEach((detail) {
          detail['isSelect'] = isSelect;
        });
      });
      update();
    } else {
      Tools.showToast("没东西可选");
    }
  }

  //选择
  void choise(String id, bool select) {
    if (!select && selectedAll) {
      //自动取消全选
      selectedAll = false;
    }
    for (var item in data) {
      List<Map<String, dynamic>> viewGoods = List<Map<String, dynamic>>.from(item['list']);
      for (var detail in viewGoods) {
        if (detail['id'] == id) {
          detail['isSelect'] = select == true ? 1 : 0;
        }
      }
    }
    update();
  }

  //编辑控制
  void edit() {
    if (!showButton) {
      showButton = true;
      actionTxt = '完成';
    } else {
      showButton = false;
      actionTxt = '编辑';
    }
    update();
  }

  //收藏
  void collect() {
    List<String> ids = getNowChoiseGoodsIds();
    if (ids.isEmpty) {
      Tools.showToast("请选择要收藏的商品");
    } else {
      //查询出对应的商品id
      Network.collectMut(ids).then((value) {
        if (value) {
          Tools.showToast("收藏成功");
          showButton = false;
          if (selectedAll == true) {
            selectedAll = false;
          }
          actionTxt = "编辑";
          getHistory();
          update();
        }
      });
    }
  }

  //删除
  void delete() {
    List ids = getNowChoiseGoods();
    if (ids.isEmpty) {
      Tools.showToast("请选择要删除的记录");
    } else {
      Tools.showModal(
        content: "确定要删除选中的这${ids.length}条记录么?",
        onConfirm: (tag) {
          Network.deleteHistory(ids).then((value) {
            if (value) {
              Tools.showToast("删除成功");
              SmartDialog.dismiss(tag: tag);
              getHistory();
            }
          });
        },
        onCancel: (tag) => {SmartDialog.dismiss(tag: tag)},
      );
    }
  }

  //获取当前选中的记录id
  List<String> getNowChoiseGoods() {
    List<String> ids = [];
    if (data.isNotEmpty) {
      for (var item in data) {
        List<Map<String, dynamic>> viewGoods = List<Map<String, dynamic>>.from(item['list']);
        for (var detail in viewGoods) {
          if (detail['isSelect'] == 1) {
            ids.add(detail['id']);
          }
        }
      }
    }
    return ids;
  }

  //获取当前选中的记录id
  List<String> getNowChoiseGoodsIds() {
    List<String> ids = [];
    if (data.isNotEmpty) {
      for (var item in data) {
        List<Map<String, dynamic>> viewGoods = List<Map<String, dynamic>>.from(item['list']);
        for (var detail in viewGoods) {
          if (detail['isSelect'] == 1) {
            ids.add(detail['goodsId'].toString());
          }
        }
      }
    }
    return ids;
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    // selectedAll = false;//刷新取消全选
    // // page = 1;
    // getHistory();
    // baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    // // if(hasNextPage==true){
    //   super.onPageLoading();
    //   page += 1;
    //   getHistory();
    //   baseRefreshCtr.loadComplete();
    //
    // // }
    selectedAll = false; //刷新取消全选
    // page = 1;
    getHistory();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
    baseRefreshCtr.loadComplete();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
